import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shishi/bean/njjzw.dart';

import '../Colors.dart';

class NaoJingJiZhuan extends StatefulWidget {
  @override
  _NaoJingJiZhuanState createState() => _NaoJingJiZhuanState();
}

class _NaoJingJiZhuanState extends State<NaoJingJiZhuan> {
  int _page = 1;
  String keyword = '';
  List<JZWS> _list = <JZWS>[];
  String answer;
  int _pages;

  void GetJiZhuanWang() async {
    try {
      var response = await Dio().get(
          "https://api.jisuapi.com/jzw/search?appkey=2a7eefb0c0c6ea21&keyword=$keyword&pagenum=$_page&pagesize=5");
      print(response);
      NJJZW njjzw = new NJJZW(response.data);
      if(njjzw.status == 0) {
        _list.addAll(njjzw.result.list);
      } else {
        Fluttertoast.showToast(msg: "${njjzw.msg}");
      }

      setState(() {});
      _page++;

    } catch (e) {
      print(e);
    }
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    GetJiZhuanWang();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('脑筋急转弯'),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.lightbulb_outline,color: Colors.white,),
            onPressed: () {
              showDialog(
                  context: context,
                  builder: (context) {
                    return Container(
                      width: MediaQuery.of(context).size.width * 0.8,
                      child: CupertinoAlertDialog(
                        actions: [
                          TextButton(onPressed: (){}, child: Text("下一题"))
                        ],
                        title: Text('正确答案是'),
                        content: Text(''),
                      ),
                    );
                  });
            },
          )
        ],
      ),
      body: body(),
    );
  }

  Widget body() {
    if (_list.length > 0) {
      return PageView.builder(
        itemCount: _list.length,
        scrollDirection: Axis.vertical,
        onPageChanged: (v){
          _pages = v;
          if((v+1)%5 == 0) {
            GetJiZhuanWang();
          }
        },
        itemBuilder: (context, index) {
          return Card(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Container(
                  child: Text(
                    '${_list[index].content}',
                    style: TextStyle(fontSize: 26, fontFamily: 'kai'),
                  ),
                ),
                const SizedBox(
                  height: 100,
                ),
                Container(
                  padding: EdgeInsets.all(20),
                  child: TextField(
                    
                    onChanged: (v) {
                      answer = v;
                    },
                    decoration: InputDecoration(
                      labelText: '请输入你的答案',
                    ),
                  ),
                ),
                Container(
                  width: MediaQuery.of(context).size.width * 0.8,
                  child: ElevatedButton(
                    style: ButtonStyle(
                      shape: MaterialStateProperty.all(StadiumBorder())
                    ),
                    child: Text('确定'),
                    onPressed: () {
                      if (_list[index].answer == answer) {
                        showDialog(
                          context: context,
                          builder: (context) {
                            return Container(
                              width: MediaQuery.of(context).size.width,
                              child: CupertinoAlertDialog(
                                title: Text('回答正确'),
                                actions: <Widget>[
                                  RaisedButton(
                                    child: Text('确定'),
                                    onPressed: () {
                                      Navigator.of(context).pop();
                                    },
                                  )
                                ],
                              ),
                            );
                          }
                        );
                      } else {
                        showDialog(
                            context: context,
                            builder: (context) {
                              return Container(
                                width: MediaQuery.of(context).size.width,
                                child: CupertinoAlertDialog(
                                  title: Text('回答错误'),
                                  actions: <Widget>[
                                    RaisedButton(
                                      child: Text('确定'),
                                      onPressed: () {
                                        Navigator.of(context).pop();
                                      },
                                    )
                                  ],
                                ),
                              );
                            }
                        );
                      }
                    },
                  ),
                )
              ],
            ),
          );
        },
      );
    } else {
      return SpinKitRipple(
        color: Cos.zhutise,
      );
    }
  }
}
